<template>
  <div style="width: 94%;margin-left: 3%;background-color: #FFFFFF">
    <div>活动补录</div>
  <div style="width: 1000px;margin: 50px 0 0 200px;">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="封面图片:" prop="cover">
        <el-upload
          class="avatar-uploader"
          action
          :http-request="selectPicUpload"
          :show-file-list="false"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.cover" :src="form.cover" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-row>
        <el-col :span="24">
          <el-form-item label="活动名:" prop="activityName">
            <el-input class="input" v-model="form.activityName" placeholder="请输入活动名"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="活动开始时间:" prop="startDatetime">
            <el-date-picker clearable
                            v-model="form.startDatetime"
                            type="datetime"
                            default-time="12:00:00"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="请选择活动开始时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动结束时间:" prop="endDatetime">
            <el-date-picker clearable
                            v-model="form.endDatetime"
                            type="datetime"
                            default-time="12:00:00"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="请选择活动结束时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="总人数限制:" prop="maxCount">
            <el-input class="input" v-model="form.maxCount" placeholder="请输入总人数限制"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报名截止时间:" prop="signEndDatetime">
            <el-date-picker clearable
                            v-model="form.signEndDatetime"
                            type="datetime"
                            default-time="12:00:00"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="请选择报名截止时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="主办方" prop="organizer">
            <el-input class="input" v-model="form.organizer[0].name" placeholder="请输入主办方名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="队伍选择:" prop="teamId">
            <el-select v-model="form.teamId" @change="teamList" filterable placeholder="请选择">
              <el-option
                v-for="item in duiwoptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="联系人:" prop="maxCount">
            <el-input class="input" v-model="form.organizer[0].linkmanName" placeholder="请输入联系人姓名"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话:" prop="workHour">
            <el-input class="input" v-model="form.organizer[0].phone" placeholder="请输入主办方手机号"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="活动地点:" prop="address">
        <el-input v-model="form.address" placeholder="请输入活动地点"/>
      </el-form-item>
      <el-form-item label="条件说明:" prop="conditionRequire">
        <el-input v-model="form.conditionRequire" type="textarea" :rows="3" placeholder="请输入内容"/>
      </el-form-item>
      <el-form-item label="活动介绍:" prop="description">
        <el-input v-model="form.description" type="textarea" :rows="3" placeholder="请输入内容" width="200px"/>
      </el-form-item>
      <el-form-item label="服务种类:" prop="serviceCategory">
        <el-checkbox-group v-model="checkList1">
          <el-checkbox v-for="item in categorylist[0]" @change="checkList" :label="item.name"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="服务对象:" prop="serviceObject">
        <el-checkbox-group v-model="checkList2">
          <el-checkbox v-for="item in categorylist[1]" :label="item.name"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="志愿者保障:" prop="serviceCategory">
        <el-checkbox-group v-model="checkList3">
          <el-checkbox v-for="item in categorylist[2]" :label="item.name"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <div>
        <el-form-item label="岗位需求:" prop="jobUserVOS">
          <div v-for="(item,index) in form.jobUserVOS">
            <div style="display: flex;margin-top: 30px">
              <div>名称:</div>
              <el-input style="width: 100px;margin-left: 10px" v-model="item.jobName"/>
              <div style="margin-left: 30px">工时:</div>
              <el-input style="width: 100px;margin-left: 10px" v-model="item.workHour"/>
              <div style="margin-left: 30px">积分 :</div>
              <el-input style="width: 100px;margin-left: 10px" v-model="item.point"/>
              <!--          删除选项-->
              <div style="margin-left: 30px">志愿者 :</div>
              <el-select v-model="item.userIdList" @change="AddSelect1(index)" multiple filterable placeholder="请选择">
                <el-option
                  v-for="item in userList"
                  :key="item.uid"
                  :label="item.realName"
                  :value="item.uid"
                >
                </el-option>
              </el-select>
<!--              <el-button style="margin-left: 10px;width: 35px;height: 35px" type="primary" icon="el-icon-user"-->
<!--                         size="mini" circle @click="AddSelect(index)"-->
<!--              ></el-button>-->
              <el-button style="margin-left: 10px;width: 35px;height: 35px" type="danger" icon="el-icon-delete"
                         size="mini" circle @click="deleteSelect(index,4)"
              ></el-button>
            </div>
            <div v-for="(n,p) in item.jobUsers" style="display: flex;margin-top: 30px">
              <div>志愿者:</div>
              <el-select v-model="n.userId" filterable placeholder="请选择">
                <el-option
                  v-for="item in userList"
                  :key="item.uid"
                  :label="item.realName"
                  :value="item.uid"
                >
                </el-option>
              </el-select>
              <div style="margin-left: 30px">工时:</div>
              <el-input style="width: 100px;margin-left: 10px" v-model="n.workHour"/>
              <div style="margin-left: 30px">积分:</div>
              <el-input style="width: 100px;margin-left: 10px" v-model="n.point"/>
              <el-button style="margin-left: 10px;width: 35px;height: 35px" type="danger" icon="el-icon-delete"
                         size="mini" circle @click="deleteUser(index,p)"
              ></el-button>
            </div>
          </div>
        </el-form-item>
        <el-button
          style="margin:-20px 0 0 42px"
          type="primary"
          plain
          size="mini"
          @click="jobAdd(4)"
        >新增岗位
        </el-button>
      </div>
      <!--        新增特色-->
      <div style="display: flex">
        <div>
          <el-form-item label="活动特色:" prop="feature">
            <div v-for="(item,index) in form.feature" style="display: flex;margin-top: 30px">
              <div>特色名称:</div>
              <el-input style="width: 100px;margin-left: 10px" v-model="item.name"/>
              <!--          删除选项-->
              <el-button style="margin-left: 10px;width: 35px;height: 35px" type="danger" icon="el-icon-delete"
                         size="mini" circle @click="deleteSelect(index,2)"
              ></el-button>
            </div>
          </el-form-item>
          <el-button
            style="margin:-10px 0 0 42px"
            type="primary"
            plain
            size="mini"
            @click="jobAdd(2)"
          >新增特色
          </el-button>
        </div>
      </div>
      <div>
        <el-form-item label="举办场次:" prop="organizer">
          <div v-for="(item,index) in form.session">
            <div style="display: flex;margin-top: 20px">
              <div style="margin-left: 30px;">场次名称:</div>
              <el-input style="width: 100px;margin-left: 10px" v-model="item.sessionName"/>
              <el-button style="margin-left: 300px;width: 35px;height: 35px" type="danger" icon="el-icon-delete"
                         size="mini" circle @click="deleteSelect(index,3)"
              ></el-button>
            </div>
            <div style="display: flex;margin-top: 20px">
              <div style="margin-left: 30px">开始时间：</div>
              <el-date-picker
                v-model="item.startDatetime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                default-time="12:00:00"
              >
              </el-date-picker>
              <div style="margin-left: 30px">结束时间：</div>
              <el-date-picker
                v-model="item.endDatetime"
                type="datetime"
                placeholder="选择日期时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                default-time="12:00:00"
              >
              </el-date-picker>

            </div>

            <!--          删除选项-->
          </div>
        </el-form-item>
        <el-button
          style="margin:-10px 0 0 42px"
          type="primary"
          plain
          size="mini"
          @click="jobAdd(3)"
        >新增场次
        </el-button>
      </div>
    </el-form>
    <div style="display: flex;justify-content: center;margin-top: 30px">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
  </div>
</template>

<script>
import { imgUrl } from '@/utils/request'
import { listUser, teamlist } from '@/api/user/user'
import {
  activitynormalList,
  addActivity,
  delActivity, deljob, deny, enterActivity,
  getActivity,
  listActivity, pass,
  updateActivity, uploadPic
} from '@/api/activity/activity'

export default {
  name: 'additional',
  data(){
    return{
      // 图片前缀
      imgUrl: imgUrl,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动列表表格数据
      activityList: [],
      // 弹出层标题
      title: '补录',
      // 是否显示弹出层
      open: false,
      // 查询参数
      userId:[],
      // for
      currten: {
        pageNum: 1,
        pageSize: 10
      },
      userqueryParams: {
        // pkId: null,
        // phone: null,
        // nickName: null,

        realName: null,
        role: 1,
        teamId: null,
        account: null,
        contactAddress: null

        // company: null,
        // politicalIdentityId: null,
        // joinDescription: null,
        // avatar: null,
        // point: null,
        // workHours: null,
        // idNumber: null,
        // birthDate: null,
        // sex: null,
        // forte: null
      },
      //分页
      column: {
        pageSize: null,
        pageNum: null
      },
      queryParams: {
        activityName: null,
        cover: null,
        startDatetime: null,
        endDatetime: null,
        signEndDatetime: null,
        maxCount: null,
        conditionRequire: null,
        description: null,
        address: null,
        status: null,
        job: null,
        teamId: null,
        session: null,
        feature: null,
        welfare: null,
        organizer: null,
        point: null
      },
      // 志愿者列表
      userList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cover: [
          { required: true, message: '请输入活动图片', trigger: 'change' }
        ],
        serviceCategory: [
          { required: true, message: '请选择服务种类', trigger: 'change' }
        ],
        serviceObject: [
          { required: true, message: '请选择服务对象', trigger: 'change' }
        ],
        serviceSafeguard: [
          { required: true, message: '请选择志愿者保障', trigger: 'change' }
        ],
        activityName: [
          { required: true, message: '请输入活动名称', trigger: 'change' }
        ],
        teamId: [
          { required: true, message: '请选择队伍列表', trigger: 'change' }
        ],
        maxCount: [
          { required: true, message: '最大人数不能为空', trigger: 'change' }
        ],
        endDatetime: [
          { required: true, message: '活动结束时间必须选择', trigger: 'change' }
        ],
        startDatetime: [
          { required: true, message: '活动开始时间不能为空', trigger: 'change' }
        ],
        description: [
          { required: true, message: '活动开始内容不能为空', trigger: 'change' }
        ],
        address: [
          { required: true, message: '地点不能为空', trigger: 'change' }
        ],
        conditionRequire: [
          { required: true, message: '请输入条件说明', trigger: 'change' }
        ],
        signEndDatetime: [
          { required: true, message: '活动截止时间必须选择', trigger: 'change' }
        ],
        job: [
          { type: 'array', required: true, message: '请添加岗位需求', trigger: 'change' }
        ],
        feature: [
          { type: 'array', required: true, message: '请添加活动特色', trigger: 'change' }
        ],
        session: [
          { type: 'array', required: true, message: '请添加活动场次', trigger: 'change' }
        ]
      },
      duiwoptions: [],
      // 种类列表
      categorylist: [],
      checkList1: [],
      checkList2: [],
      checkList3: []
    }
  },
  created() {
    this.getcategory()
    this.getList()
    this.gettemn()
    this.reset()
  },
  methods: {
    // 获取队伍
    gettemn() {
      teamlist().then(res => {
        this.duiwoptions = res
        console.log(this.duiwoptions)
      })
    },
    // 表单
    getcategory() {
      activitynormalList().then(res => {
        this.categorylist = res
      })
    },
    /** 查询活动列表列表 */
    getList() {
      this.loading = true
      listActivity(this.queryParams, this.currten).then(response => {
        this.activityList = response.pageData
        this.total = Number(response.total)
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        jobUserVOS: [],
        activityName: null,
        cover: null,
        startDatetime: null,
        endDatetime: null,
        signEndDatetime: null,
        maxCount: 0,
        conditionRequire: null,
        description: null,
        address: null,
        status: null,
        job: [],
        teamId: null,
        session: [],
        feature: [],
        welfare: [],
        serviceCategory: [{
          id: 0,
          name: '',
          prefix: ''
        }],
        serviceObject: [{
          id: 0,
          name: '',
          prefix: ''
        }],
        serviceSafeguard: [{
          id: 0,
          name: '',
          prefix: ''
        }],
        organizer: [
          {
            id: 0,
            name: '',
            prefix: '',
            linkmanName: '',
            phone: ''
          }
        ],
        workHour: 0
      }
    },
    checkList(e) {
      console.error(e)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pkId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加活动列表'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const pkId = row.pkId || this.ids
      getActivity(pkId).then(response => {
        this.form = response
        this.open = true
        this.title = '修改活动列表'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.form.serviceCategory = this.categorylist[0].filter(item => this.checkList1.includes(item.name))
        .map(item => ({ name: item.name, id: item.pkId, prefix: '' }))
      this.form.serviceObject = this.categorylist[1].filter(item => this.checkList2.includes(item.name))
        .map(item => ({ name: item.name, id: item.pkId, prefix: '' }))
      this.form.serviceSafeguard = this.categorylist[2].filter(item => this.checkList3.includes(item.name))
        .map(item => ({ name: item.name, id: item.pkId, prefix: '' }))
      console.log(this.checkList1, '112121212', this.form.serviceCategory)
      this.$refs['form'].validate(valid => {
        if (valid) {
          enterActivity(this.form).then(response => {
              this.$modal.msgSuccess('补录成功')
              this.reset()
            })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pkIds = row.pkId || this.ids
      this.$modal.confirm('是否确认删除该活动？').then(function() {
        return delActivity(pkIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.title = '补录'
      this.open = true
    },
    // 上传图片
    selectPicUpload(obj) {
      console.log(obj)
      let fd = new FormData() //参数的格式是formData格式的
      fd.append('file', obj.file) //参数
      console.log(fd)
      uploadPic(fd, { modularName: 'ACTIVITY' }).then(res => {
        this.form.cover = res
      })
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isLt2M
    },
    // 岗位新增
    jobAdd(e) {
      if (e === 1) {
        this.form.job.push({
          'id': 0,
          'jobCount': 1,
          'jobName': '',
          'jobRequire': '',
          'jobSignedCount': 0,
          'workHour': 0
        })
      } else if (e === 2) {
        this.form.feature.push(
          {
            'id': 0,
            'name': '',
            'prefix': ''
          })
      } else if (e === 3) {
        this.form.session.push(
          {
            'endDatetime': '',
            'id': 0,
            'sessionName': '',
            'startDatetime': ''
          })
      } else if (e === 4) {
        this.form.jobUserVOS.push(
          {
            'id': 0,
            'jobCount': 0,
            'jobName': '',
            'jobRequire': '',
            'jobUsers': [],
            userIdList:[],
            'point': 0,
            'workHour': 0
          })
      }
      console.log(this.form.job)
    },
    AddSelect1(index) {
      console.log('11111111',this.form.jobUserVOS[index].userIdList)
      this.form.jobUserVOS[index].jobUsers = []
      this.form.jobUserVOS[index].userIdList.forEach(item =>{
        this.form.jobUserVOS[index].jobUsers.push({
          'userId': item,
          point:this.form.jobUserVOS[index].point,
          'workHour': this.form.jobUserVOS[index].workHour
        })
      })

    },
    AddSelect(index,e){
      console.error(index,e,'askldajd')
      this.form.jobUserVOS[index].jobUsers.push({
        'userId': e,
        'workHour': 0
      })
    },
    // 删除按钮
    deleteSelect(index, e) {
      this.$modal.confirm('是否确认删除此选项?').then(function() {
      }).then(() => {
        if (e === 1) {
          console.error(index, this.title == '添加活动列表')
          if (this.title == '添加活动列表') {
            console.error(index)
            this.form.job.splice(index, 1)
          } else {
            deljob(this.form.job[index].id).then(res => {
              if (res.code === 200) {
                this.form.job.splice(index, 1)
                this.$modal.msgSuccess('删除成功')
              }
            })
          }
        } else if (e === 2) {
          this.form.feature.splice(index, 1)
          this.$modal.msgSuccess('删除成功')
        } else if (e === 3) {
          this.form.session.splice(index, 1)
          this.$modal.msgSuccess('删除成功')
        } else if (e === 4) {
          this.form.jobUserVOS.splice(index, 1)
          this.$modal.msgSuccess('删除成功')
        }
        //如果是服务类型，删掉时间段
        if (this.form.productCategory == 1) {
          this.selectTime.splice(index, 1)
        }

      }).catch(() => {
      })
    },
    // 补录删除志愿者
    deleteUser(e, p) {
      this.form.jobUserVOS[e].jobUsers.splice(p, 1)
      this.$modal.msgSuccess('删除成功')
    },
    handleUpdatepass(e) {
      pass(e.pkId).then(res => {
        if (res.code === 200) {
          this.$modal.msgSuccess('同意成功')
        }
      })
    },
    handleDeletedeny(e) {
      deny(e.pkId).then(res => {
        if (res.code === 200) {
          this.$modal.msgSuccess('拒绝成功')
        }
      })
    },
    teamList(e) {
      console.error(e)
      this.userqueryParams.teamId = e
      listUser(this.userqueryParams, this.column).then(response => {
        this.userList = response.pageData
      })
    }
  }
}
</script>

<style scoped>

</style>
